﻿using Ah.Testudin.Engine.Content;
using Ah.Testudin.Engine.Events;
using Ah.Testudin.Engine.Graphics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Ah.Testudin.Engine.UI.Elements.Inputs
{
    public delegate void HandleButtonClickDelegate();

    public class ButtonScreenElement : ScreenElementBase
    {
        #region Private Member Variables

        private int _cursorAreaPadding = 1;

        private ButtonState _previousPrimaryMouseButtonState;

        private ITestudinRenderer _renderer;

        private Texture2D _buttonTexture;

        private Texture2D _highlightedButtonTexture;

        private HandleButtonClickDelegate _handleButtonClickDelegate;

        private bool _highlighted;

        #endregion

        #region Constructors

        public ButtonScreenElement(
            Vector2 position,
            int width,
            int height,
            Texture2D buttonTexture,
            Texture2D highlightedButtonTexture,
            HandleButtonClickDelegate handleButtonClickDelegate)
            : base(position, width, height)
        {
            _buttonTexture = buttonTexture;

            _highlightedButtonTexture = highlightedButtonTexture;

            _handleButtonClickDelegate = handleButtonClickDelegate;

            _previousPrimaryMouseButtonState = ButtonState.Released;

            _renderer = GraphicsDeviceRendererSingleton.Instance;

            _highlighted = false;
        }

        #endregion

        #region ScreenElementBase

        public override short ZOrder
        {
            get { return 0; }
        }

        public override bool Visible
        {
            get { return true; }
        }

        public override bool ProcessInput()
        {
            bool inputProcessed = false;

            MouseState mouseState = Mouse.GetState();

            if ((mouseState.LeftButton == ButtonState.Released) &&
                (_previousPrimaryMouseButtonState == ButtonState.Pressed))
            {
                if (_highlighted)
                {
                    _handleButtonClickDelegate();

                    inputProcessed = true;
                }
            }

            _previousPrimaryMouseButtonState = mouseState.LeftButton;

            return inputProcessed;
        }

        public override void Update(GameTime gameTime)
        {
            _highlighted = IsHighlighted();
        }

        public override void Render()
        {
            _renderer.BeginSpriteBatch();

            _renderer.RenderSprite(
                new Rectangle(
                    (int)_position.X,
                    (int)_position.Y,
                    _width,
                    _height),
                _highlighted
                    ? _highlightedButtonTexture
                    : _buttonTexture);

            _renderer.EndSpriteBatch();
        }

        #endregion

        #region Private Methods

        private bool IsHighlighted()
        {
            bool highlighted = false;

            MouseState mouseState = Mouse.GetState();

            Rectangle cursorRectangle = new Rectangle(
                    mouseState.X - _cursorAreaPadding,
                    mouseState.Y - _cursorAreaPadding,
                    _cursorAreaPadding * 2,
                    _cursorAreaPadding * 2);

            Rectangle buttonRectangle = new Rectangle(
                (int)_position.X,
                (int)_position.Y,
                _width,
                _height);

            if (cursorRectangle.Intersects(buttonRectangle))
            {
                highlighted = true;
            }

            return highlighted;
        }

        #endregion
    }
}
